package com.junqi.sys.application.controller.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.junqi.sys.common.utils.MessageUtil;
import com.junqi.sys.common.utils.SHA1Utils;
import com.junqi.sys.domain.handler.WxChatMsgFactory;
import com.junqi.sys.domain.handler.WxChatMsgHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/wx")
@Slf4j
public class WxController {


    @Resource
    private WxChatMsgFactory wxChatMsgFactory;
    private static final String token = "adfagq3wtczxvzx";

    private static final String AppID="wx61507574c55d2daf";
    private static final String AppSecret="6a37a0d5677b03b6619d74f0cf2235db";

    /**
     * 回调消息校验
     */
    @GetMapping("/callback")
    public void callback(@RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("echostr") String echostr, HttpServletResponse response) throws IOException {
        log.info("get验签请求参数：signature:{}，timestamp:{}，nonce:{}，echostr:{}",
                signature, timestamp, nonce, echostr);
        String shaStr = SHA1Utils.getSHA1(token, timestamp, nonce, "");
        log.info("get验签请求参数：echostr:{}",echostr);
        if (signature.equals(shaStr)) {
            response.getWriter().write(echostr);
        }else{
            response.getWriter().write("unknown");
        }
        response.getWriter().flush();
        response.getWriter().close();
    }

    @PostMapping(value = "/callback")
    public void callback(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //log.info("接收到微信消息：requestBody：{}", requestBody);
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Map<String, String> messageMap = MessageUtil.parseXml(request);
        String msgType = messageMap.get("MsgType");
        String event = messageMap.get("Event") == null ? "" : messageMap.get("Event");
        log.info("msgType:{},event:{}", msgType, event);
        StringBuilder sb = new StringBuilder();
        sb.append(msgType);
        if (StrUtil.isNotBlank(event)) {
            sb.append(".");
            sb.append(event);
        }
        String msgTypeKey = sb.toString();
        WxChatMsgHandler wxChatMsgHandler = wxChatMsgFactory.getHandlerByMsgType(msgTypeKey);
        if (ObjectUtil.isNull(wxChatMsgHandler)) {
            response.getWriter().write("unknown");
        }else{
            String replyContent = wxChatMsgHandler.dealMsg(messageMap);
            log.info("replyContent:{}", replyContent);
            response.getWriter().write(replyContent);
        }
        response.getWriter().flush();
        response.getWriter().close();
    }

}
